package com.example.neteasecloudmusic_kotlin

import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.viewpager2.widget.ViewPager2
import com.example.neteasecloudmusic_kotlin.ui.FragmentAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import java.lang.reflect.Field
import kotlin.math.roundToInt

class MainActivity : AppCompatActivity() {

    protected lateinit var viewPager: ViewPager2
    protected lateinit var tabLayout: TabLayout

    // Todo: tab选中时 对应的文字放大 即酷狗音乐Tab显示方式
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val decorView = window.decorView
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        window.statusBarColor = Color.TRANSPARENT
        setContentView(R.layout.activity_main)
        viewPager = findViewById(R.id.viewPager)
        tabLayout = findViewById(R.id.tabLayout)
        viewPager.adapter = FragmentAdapter(this)
        tabLayout.post(
            Runnable {
                setTabLayoutWidth(12.toFloat(), 12.toFloat(), tabLayout, this)
            }
        )
        // 自定义选中图标的颜色
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val position = tab?.position
                if (position != 0) {
                    val filter = PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN)
                    tab?.icon?.colorFilter = filter
                }
            }
            override fun onTabUnselected(tab: TabLayout.Tab?) {
                val position = tab?.position
                if (position != 0) {
                    val filter = PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN)
                    tab?.icon?.colorFilter = filter
                }
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
        TabLayoutMediator(tabLayout, viewPager)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                { tab, position ->
            when (position) {
                0 -> {
                    tab.text = "发现"
                    tab.setIcon(R.drawable.ic_discovery)
                }
                1 -> {
                    tab.text = "播客"
                    tab.setIcon(R.drawable.ic_player)
                }
                2 -> {
                    tab.text = "我的"
                    tab.setIcon(R.drawable.ic_mine)
                }
                3 -> {
                    tab.text = "关注"
                    tab.setIcon(R.drawable.ic_followed)
                }
                4 -> {
                    tab.text = "云村"
                    tab.setIcon(R.drawable.ic_cloudhometown)
                }
            }
        }.attach()
    }

    /**
     * 修改indicator宽度
     */
    private fun setTabLayoutWidth(
        left: Float,
        right: Float,
        tabLayout: TabLayout?,
        activity: MainActivity
    ) {
        val leftDip  = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, left, Resources.getSystem().displayMetrics)
        val rightDip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, right, Resources.getSystem().displayMetrics)
//        val bottomDip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.toFloat(), Resources.getSystem().displayMetrics)
        var textViewField : Field
        val slidingTabIndicator = tabLayout!!::class.java.getDeclaredField("slidingTabIndicator")
        slidingTabIndicator.isAccessible = true
        val mTabStrip : LinearLayout = slidingTabIndicator.get(tabLayout) as LinearLayout
        // Todo: no reflect method
        for (i in 0..(mTabStrip.childCount - 1)) {
            val tabView = mTabStrip.getChildAt(i)
            textViewField = tabView::class.java.getDeclaredField("textView")
            textViewField.isAccessible = true
            val mTextView = textViewField.get(tabView) as TextView
            tabView.setPadding(0, 0, 0,0)
            var width = mTextView.width
            if (width == 0) {
                mTextView.measure(0, 0)
                width = mTextView.measuredWidth
            }
            var params = tabView.layoutParams as LinearLayout.LayoutParams
            params.width = width
//            params.bottomMargin = bottomDip.toInt()
            params.leftMargin = leftDip.toInt()
            params.rightMargin = rightDip.toInt()
            tabView.layoutParams = params
            tabView.invalidate()
        }

    }


}



